home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / python2.5 / hmac.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-05-11  |  3.8 KB  |  112 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''HMAC (Keyed-Hashing for Message Authentication) Python module.
  5.  
  6. Implements the HMAC algorithm as described by RFC 2104.
  7. '''
  8.  
  9. def _strxor(s1, s2):
  10.     '''Utility method. XOR the two strings s1 and s2 (must have same length).
  11.     '''
  12.     return ''.join(map((lambda x, y: chr(ord(x) ^ ord(y))), s1, s2))
  13.  
  14. digest_size = None
  15. _secret_backdoor_key = []
  16.  
  17. class HMAC:
  18.     '''RFC2104 HMAC class.
  19.  
  20.     This supports the API for Cryptographic Hash Functions (PEP 247).
  21.     '''
  22.     
  23.     def __init__(self, key, msg = None, digestmod = None):
  24.         '''Create a new HMAC object.
  25.  
  26.         key:       key for the keyed hash object.
  27.         msg:       Initial input for the hash, if provided.
  28.         digestmod: A module supporting PEP 247.  *OR*
  29.                    A hashlib constructor returning a new hash object.
  30.                    Defaults to hashlib.md5.
  31.         '''
  32.         if key is _secret_backdoor_key:
  33.             return None
  34.         
  35.         if digestmod is None:
  36.             import hashlib
  37.             digestmod = hashlib.md5
  38.         
  39.         if callable(digestmod):
  40.             self.digest_cons = digestmod
  41.         else:
  42.             
  43.             self.digest_cons = lambda d = ('',): digestmod.new(d)
  44.         self.outer = self.digest_cons()
  45.         self.inner = self.digest_cons()
  46.         self.digest_size = self.inner.digest_size
  47.         blocksize = 64
  48.         ipad = '6' * blocksize
  49.         opad = '\\' * blocksize
  50.         if len(key) > blocksize:
  51.             key = self.digest_cons(key).digest()
  52.         
  53.         key = key + chr(0) * (blocksize - len(key))
  54.         self.outer.update(_strxor(key, opad))
  55.         self.inner.update(_strxor(key, ipad))
  56.         if msg is not None:
  57.             self.update(msg)
  58.         
  59.  
  60.     
  61.     def update(self, msg):
  62.         '''Update this hashing object with the string msg.
  63.         '''
  64.         self.inner.update(msg)
  65.  
  66.     
  67.     def copy(self):
  68.         """Return a separate copy of this hashing object.
  69.  
  70.         An update to this copy won't affect the original object.
  71.         """
  72.         other = HMAC(_secret_backdoor_key)
  73.         other.digest_cons = self.digest_cons
  74.         other.digest_size = self.digest_size
  75.         other.inner = self.inner.copy()
  76.         other.outer = self.outer.copy()
  77.         return other
  78.  
  79.     
  80.     def digest(self):
  81.         '''Return the hash value of this hashing object.
  82.  
  83.         This returns a string containing 8-bit data.  The object is
  84.         not altered in any way by this function; you can continue
  85.         updating the object after calling this function.
  86.         '''
  87.         h = self.outer.copy()
  88.         h.update(self.inner.digest())
  89.         return h.digest()
  90.  
  91.     
  92.     def hexdigest(self):
  93.         '''Like digest(), but returns a string of hexadecimal digits instead.
  94.         '''
  95.         return []([ hex(ord(x))[2:].zfill(2) for x in tuple(self.digest()) ])
  96.  
  97.  
  98.  
  99. def new(key, msg = None, digestmod = None):
  100.     """Create a new hashing object and return it.
  101.  
  102.     key: The starting key for the hash.
  103.     msg: if available, will immediately be hashed into the object's starting
  104.     state.
  105.  
  106.     You can now feed arbitrary strings into the object using its update()
  107.     method, and can ask for the hash value at any time by calling its digest()
  108.     method.
  109.     """
  110.     return HMAC(key, msg, digestmod)
  111.  
  112.